package sma.iss.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * utility for ISS csv files.
 * 
 * @author smarsoll
 * 
 */
public class CsvIss {
	/** Logger for this class. */
	private final static Logger LOGGER = LoggerFactory.getLogger(CsvIss.class);

	public static final String DIR_BASE_NAME = "result";
	public static final String DELIM = "_";

	/**
	 * 
	 * @param beta
	 * @param yaw
	 * @return "result/try/b_70.0_y_0.0_s160442.csv"
	 */
	public static final String getCsvTryName(double beta, double yaw) {
		DateFormat dateFormat = new SimpleDateFormat("MMddHHmm");
		return DIR_BASE_NAME + "/try/b" + DELIM + beta + DELIM + "y" + DELIM + yaw + DELIM + dateFormat.format(new Date()) + "s.csv";
	}

	public static final void generateAllCsvToJava() throws IOException {
		File dir = new File(DIR_BASE_NAME + "/ok");

		// the output
		File javaEnd = new File(DIR_BASE_NAME + "/generated/java-end.txt");
		FileOutputStream fos = new FileOutputStream(javaEnd);
		Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
		writer.write("// code generated by SMa. Paste it in TemplateISS\n");
		writer.write("// ");
		writer.write((new Date()).toString());
		writer.write("\n");

		List<File> allCsv = new ArrayList<File>(Arrays.asList(dir.listFiles()));
		for (Iterator iterator = allCsv.iterator(); iterator.hasNext();) {
			File file = (File) iterator.next();
			if (!file.isFile()) {
				iterator.remove();
			}
		}

		// call methods
		writer.write("	private void initData() {\n");
		for (File csvFile : allCsv) {
			String fileName = csvFile.getName();
				LOGGER.info("Processing {}", fileName);
				String[] strValues = fileName.split(DELIM);
				double beta = Double.parseDouble(strValues[1]);
				String methodName = getMethodeName(beta);
				writer.write(methodName);
				writer.write("();\n");
		}
		writer.write("}\n");

		for (File csvFile : allCsv) {
			String[] strValues = csvFile.getName().split(DELIM);
			double beta = Double.parseDouble(strValues[1]);
			double yaw = Double.parseDouble(strValues[3]);
			LOGGER.info("beta={}", beta);
			dumpDataInFile(writer, beta, yaw, csvFile);
		}
		writer.write("}\n");

		writer.flush();
		writer.close();

		// return allCsv;
	}

	private static String getMethodeName(double beta) {
		int betaInt = (int) Math.abs(beta);
		String m = "";
		if (beta < 0.0) {
			m = "M";
		}
		return "initData" + m + betaInt;
	}

	private static void dumpDataInFile(Writer writer, double beta, double yaw, File csvFile) throws IOException {
		String methodName = getMethodeName(beta);
		// private void initDataM72() {
		writer.write("private void ");
		writer.write(methodName);
		writer.write("() {\n");

		writer.write("	double betaData = ");
		writer.write(Double.toString(beta));
		writer.write(";\n");
		if ((beta == 70.0) || (beta == -72.0) || true) {
			LOGGER.info("dumpDataInFile");
			// log("initDataM72");
			writer.write("log(\"");
			writer.write(methodName);
			writer.write("\");\n");
			// mapYaw.put(betaData, 0.0);
			writer.write(" mapYaw.put(betaData, ");
			writer.write(Double.toString(yaw));
			writer.write(");\n");
			writer.write("	// 92 lines\n		double[][] lines = new double[92][];\n");

			// open CSV file
			FileInputStream fin = new FileInputStream(csvFile);
			BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
			// first line ignored
			myInput.readLine();
			String line;
			for (int i = 0; i < 92; i++) {
				line = myInput.readLine();
				// lines[0] = new double[] { 72.0, 0.0, 181.77575173184948, 0.0, 7.993605777301127E-15,
				// -0.06521739130434782, 342.0, 0.0, 162.0,0.0, 15.0, 0.0, 195.0, 0.0, 15.0, 0.0, 195.0, 0.0, 345.0,
				// 0.0,
				// 165.0, 0.0 };
				writer.write("lines[");
				writer.write(Integer.toString(i));
				writer.write("] = new double[] { ");
				// remove first 2 values fromline (72.0, 0.0)
				line = line.substring(line.indexOf(',') + 1);
				line = line.substring(line.indexOf(',') + 1);
				writer.write(line);
				writer.write("};\n");

			}

			fin.close();
			writer.write("mapData.put(betaData, lines);\n");
		}
		writer.write("}\n");
	}

}
